package gov.kqqzyyy.monkey.clinicscreen.mytools;

import android.content.Context;
import android.os.Bundle;

import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.SynthesizerListener;
import com.iflytek.cloud.util.ResourceUtil;
import com.socks.library.KLog;


/**
 * Created by Monkey on 2016/1/8.
 */
public class SpeakTool {
    //语音合成对象
    private SpeechSynthesizer Tts = null;
    //发音人
    private String Voicer = "xiaoyan";
    //情感
    private String Emot = "";
    //缓冲进度
    private int PercentForBuffering = 0;
    //播放进度
    private int PercentForPlaying = 0;

    private Context mContext;

    private final SynthesizerListener SynListener;

    public SpeechSynthesizer getTts() {
        return Tts;
    }

    public String getVoicer() {
        return Voicer;
    }

    public void setVoicer(String voicer) {
        Voicer = voicer;
    }

    public String getEmot() {
        return Emot;
    }

    public void setEmot(String emot) {
        Emot = emot;
    }

    public int getPercentForBuffering() {
        return PercentForBuffering;
    }

    public void setPercentForBuffering(int percentForBuffering) {
        PercentForBuffering = percentForBuffering;
    }

    public int getPercentForPlaying() {
        return PercentForPlaying;
    }

    public void setPercentForPlaying(int percentForPlaying) {
        PercentForPlaying = percentForPlaying;
    }

    public Context getmContext() {
        return mContext;
    }

    public void setmContext(Context mContext) {
        this.mContext = mContext;
    }

    public SpeakTool(Context context) {
        mContext = context;
        SynListener = new SynthesizerListener() {
            @Override
            public void onSpeakBegin() {

            }

            @Override
            public void onBufferProgress(int i, int i1, int i2, String s) {

            }

            @Override
            public void onSpeakPaused() {

            }

            @Override
            public void onSpeakResumed() {

            }

            @Override
            public void onSpeakProgress(int i, int i1, int i2) {

            }

            @Override
            public void onCompleted(SpeechError speechError) {

            }

            @Override
            public void onEvent(int i, int i1, int i2, Bundle bundle) {

            }
        };
        init();
    }

//    private void init() {
////        SpeechUtility.createUtility(mContext, SpeechConstant.APPID + "=5fbda123");
//        SpeechUtility.createUtility(mContext, SpeechConstant.APPID + "=5684d8fe");
////        Tts = SpeechSynthesizer.createSynthesizer(mContext, code -> {
////            if (code != ErrorCode.SUCCESS) {
////                KLog.d("APE", "speaktool init failed,code:" + code);
////            } else {
////                KLog.d("APE", "speaktool init success,set param");
////                setParam();
////            }
////        });
//        Tts = SpeechSynthesizer.createSynthesizer(mContext, mTtsInitListener);
//    }

    private void init() {
        SpeechUtility.createUtility(mContext, SpeechConstant.APPID + "=5684d8fe");
        Tts = SpeechSynthesizer.createSynthesizer(mContext, mTtsInitListener);
    }


    /**
     * 初始化监听。
     */
    private final InitListener mTtsInitListener = code -> {
        KLog.d("APE", "init code = " + code);
        if (code != ErrorCode.SUCCESS) {
            KLog.d("APE", "初始化失败,错误码：" + code);
        } else {
            setParam();
        }
    };


    public int speak(String msg) {
        if (Tts != null) {
            KLog.d("APE", "speak:" + msg);
            return Tts.startSpeaking(msg, SynListener);
        }
        return -1;
    }

    public int speak(String msg, SynthesizerListener listener) {
        if (Tts != null) {
            return Tts.startSpeaking(msg, listener);
        }
        return -1;
    }

    public void pause() {
        if (Tts.isSpeaking()) {
            Tts.pauseSpeaking();
        }
    }

    public void resume() {
        if (Tts != null && !Tts.isSpeaking()) {
            Tts.resumeSpeaking();
        }
    }

    public void stop() {
        if (Tts != null) {
            Tts.stopSpeaking();
        }
    }

    public void destory() {
        if (Tts != null) {
            Tts.destroy();
        }
    }

    public boolean isSpeaking() {
        return Tts != null && Tts.isSpeaking();
    }


    //参数设置
    private void setParam() {
        Tts.setParameter(SpeechConstant.PARAMS, null);
        //语音速度0-100
        String speed = "40";
        Tts.setParameter(SpeechConstant.SPEED, speed);
        String volume = "100";
        Tts.setParameter(SpeechConstant.VOLUME, volume);
        //语音声调0-100
        String pitch = "40";
        Tts.setParameter(SpeechConstant.PITCH, pitch);
        //引擎类型
        Tts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_LOCAL);
        Tts.setParameter(SpeechConstant.VOICE_NAME, Voicer);
        Tts.setParameter(ResourceUtil.TTS_RES_PATH, getResourcePath());
        Tts.setParameter(SpeechConstant.STREAM_TYPE, "3");
        Tts.setParameter(SpeechConstant.KEY_REQUEST_FOCUS, "true");
    }

    //获取发音人资源路径
    private String getResourcePath() {
        String type = "tts";
        //合成通用资源
        return ResourceUtil.generateResourcePath(mContext, ResourceUtil.RESOURCE_TYPE.assets, type + "/common.jet") +
                ";" +
                ResourceUtil.generateResourcePath(mContext, ResourceUtil.RESOURCE_TYPE.assets, type + "/" + Voicer + ".jet");
    }

}
